<template>
  <view>
    <u-navbar title="企业信息"> </u-navbar>
    <formz ref="formz" :inputs="inputs" :data="data" labelWidth="220rpx"></formz>

    <bottombox>
      <u-button type="primary" :loading="loading" text="更新数据" @click="onedit"></u-button>
    </bottombox>
  </view>
</template>

<script>
import { getCompanyInfo, saveCompanyInfo } from "@/api/company/companyInfo.js";

export default {
  data() {
    return {
      data: {},
      loading: false,
      inputs: [
        {
          label: "企业名称",
          value: "enterpriseCompany",
          rules: [
            {
              required: true,
              trigger: "blur",
              message: "企业名称不能为空",
            },
          ],
        },
        {
          label: "社会信用代码",
          value: "uscc",
          rules: [
            {
              required: true,
              trigger: "blur",
              message: "统一社会信用代码不能为空",
            },
          ],
        },
        {
          label: "公司注册时间",
          value: "registerDate",
          type: "datetime",
          rules: [
            {
              required: true,
              trigger: "blur",
              message: "公司注册时间不能为空",
            },
          ],
        },
        {
          label: "注册资本",
          value: "registerCapital",
          dw: "万元",
          rules: [
            {
              required: true,
              trigger: "blur",
              message: "注册资本不能为空",
            },
            {
              validator: function (rule, value, callback) {
                if (/[0-9]*\.?[0-9]+/.test(value) === false || value < 0) {
                  callback(new Error("注册资本格式错误"));
                } else {
                  callback();
                }
              },
              trigger: "blur",
            },
          ],
        },
        {
          label: "公司网址",
          value: "enterpriseUrl",
          span: 12,
        },
        {
          label: "企业法人",
          value: "enterpriseLegalPerson",
          span: 12,
          rules: [
            {
              required: true,
              trigger: "blur",
              message: "企业法人不能为空",
            },
          ],
        },
        {
          label: "企业联系人",
          value: "enterpriseContactPerson",
          span: 12,
          rules: [
            {
              required: true,
              trigger: "blur",
              message: "企业联系人不能为空",
            },
          ],
        },
        {
          label: "联系人电话",
          value: "enterpriseContactsTel",
          span: 12,
          rules: [
            {
              required: true,
              trigger: "blur",
              message: "联系电话不能为空",
            },
            {
              validator: function (rule, value, callback) {
                if (
                  /^(?:(?:\+|00)86)?1(?:3[\d]|4[5-79]|5[0-35-9]|6[5-7]|7[0-8]|8[\d]|9[189])\d{8}$/.test(
                    value
                  ) === false
                ) {
                  callback(new Error("手机号格式错误"));
                } else {
                  callback();
                }
              },
              trigger: "blur",
            },
          ],
        },
        {
          label: "企业注册地址",
          value: "enterpriseRegAddress",
          rules: [
            {
              required: true,
              trigger: "blur",
              message: "企业注册地址不能为空",
            },
          ],
        },
        {
          label: "企业营业地址",
          value: "enterpriseBusinessAddress",
          rules: [
            {
              required: true,
              trigger: "blur",
              message: "企业营业地址不能为空",
            },
          ],
        },
        {
          label: "企业营业周期",
          value: "enterpriseBusinessCycle",
          span: 12,
        },

        {
          label: "天眼查code",
          value: "tianYanChaCode",
          span: 12,
          rules: [
            {
              required: true,
              trigger: "blur",
              message: "天眼查code不能为空",
            },
          ],
        },
        {
          label: "企业性质",
          value: "enterpriseNature",
          span: 12,
          rules: [
            {
              required: true,
              trigger: "blur",
              message: "企业性质不能为空",
            },
          ],
        },
        {
          label: "营业范围",
          value: "businessScope",
          span: 12,
          rules: [
            {
              required: true,
              trigger: "blur",
              message: "营业范围不能为空",
            },
          ],
        },
        {
          label: "邮箱",
          value: "email",
          span: 12,
        },
        {
          label: "公众号",
          value: "officialAccount",
          span: 12,
        },
        {
          label: "备注",
          value: "remark",
        },
      ],
    };
  },
  onLoad() {
    this.init();
  },
  methods: {
    async init() {
      let data = await getCompanyInfo();
      this.data = data.data;
    },
    async onedit() {
      let data = await this.$refs.formz.validate();

      this.loading = true;
      try {
        await saveCompanyInfo(data);
        this.loading = false;
        await this.$u.modal("修改成功");
        this.$u.back();
      } catch (error) {
        this.loading = false;
      }
    },
  },
};
</script>

<style>
page {
  background: #fff;
}
</style>
